<?php
/*
KAP 0.5 - sql manager - mysql
file version 2005-06-17 14:06:19

bati sur mysqlTool
*/

class waksqlManager extends wakCoreComponent {
/*----------------------------------------------------------------------------------------------
                                                                                      proprietes
----------------------------------------------------------------------------------------------*/
	var $default_parameters=array(
		'domain'=>'localhost',
		'login'=>'root',
		'pwd'=>'',
		'base'=>NULL,
		'encoding'=>NULL
	);
	var $link;
	var $log=array();
	/* log des operations, tableaux sous la forme
	'query'=>requete sql, 'ok'=>booleen, 'error'=>erreurs eventuelles, 'count'=> nbre de lignes affectees
	*/
/*----------------------------------------------------------------------------------------------
                                                                                    constructeur
----------------------------------------------------------------------------------------------*/
	function waksqlManager(& $controller) {
		// appel du constructeur parent
		$const=get_parent_class($this);
		parent::$const($controller);
		//echo wakaffvar($this->parameters,'sqlmanager_param');
		wakNeedClass("mysqltool");
		// getting singleton of mysqltool
		$this->tool=mysqltool::getSingleton($this->parameters['domain']
		,$this->parameters['login']
		,$this->parameters['pwd']
		,$this->parameters['base']
		,$this->parameters['encoding']);
		// echo wakaffvar($this->parameters,'wakSqlManager params');
	}
/*----------------------------------------------------------------------------------------------
                                                                              methodes publiques
----------------------------------------------------------------------------------------------*/
	// ouverture de la connexion (si necessaire)
	function open($base=NULL) {
		//echo affvar($this->link);
		if ($this->link) return true;
		if ($base===NULL)
			$base=$this->parameters['base'];
		if (!$this->tool->open($this->parameters['domain'],$this->parameters['login'],$this->parameters['pwd'])) {
			wakLog('sqlManager: couldn\'t connect to host '.$this->parameters['domain']);
			//trigger_error()
			return false;
		}
		//echo affvar($this->link);
		wakLog('sqlManager: connected with host '.$this->parameters['domain']);
		return true;
	}
	function setDb($base) {
		return $this->tool->setdb($base);
	}
	// fermeture de la connexion
	function close() {
		return $this->tool->close();
	}
	// renvoie des infos sur la structure
	// recup de la structure de la table
	function tableStructure($table) {
		return $this->tool->tablestructure($table);
	}
	// tableau de resultats
	function & result($sql,$indexField=NULL) {
		$result=& $this->tool->result($sql,$indexField);
		if (is_array($result))
			$show=count($result)." results";
		else
			$show=wakXmlEntities(wakaffvar($result,'',5));
		wakLog("sqlManager->result: $sql returned ".$show);
		if ($result===false)
			wakLog("mysql error was : ".mysql_error());
		return $result;
	}
	// tableau de résultat unique
	function & uniqueResult($sql) {
		$result=& $this->tool->uniqueResult($sql);
		if (is_array($result))
			$show=join(' ',array_keys($result))."[...";
		else
			$show=wakXmlEntities(wakaffvar($result));
		wakLog("sqlManager->uniqueresult: $sql returned ".$show);
		if ($result===false)
			wakLog("mysql error was : ".mysql_error());
		return $result;
	}
	// execution de requete
	function execute($sql) {
		$result=$this->tool->execute($sql);
		wakLog('sqlManager->execute: '.wakXmlEntities($sql).' returned '.wakXmlEntities(wakAffVar($result)));
		if ($result===false)
			wakLog("mysql error was : ".mysql_error());
		return $result;
	}
	// renvoie une valeur "sécurisée" à utiliser dans une requête sql.
	// ajoute des guillemets simples atour de la valeur
	function secureValue($value) {
		return $this->tool->secureValue($value);
	}
	function insertArray($table,$data=array(),$explicitNegative=false) {
		return $this->tool->insertArray($table,$data,$explicitNegative);
	}
	
	// renvoie un resumé de la dernire operation
	// mode = array|string
	function lastOpSummary($mode='string') {
		return $this->tool->lastOpSummary($mode);
	}

/*-------------------------------------------------------------------------------------------------
																												private methods
-------------------------------------------------------------------------------------------------*/
}
?>
